﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Templates
{
    public class Row : ITemplate, IXmlBuildable, IChildDeletable
	{
		public List<ControlRef> ControlRefs { get; set; }
		public List<OverflowArea> OverflowAreas { get; set; }
		public List<Strip> Strips { get; set; }

		public Row ()
		{
			this.ControlRefs = new List<ControlRef>();
			this.OverflowAreas = new List<OverflowArea>();
			this.Strips = new List<Strip>();
		}

		public Row(XmlNode node):this()
		{
            try
            {
                foreach (XmlNode crefNode in node.SelectNodes("ControlRef"))
                {
                    ControlRef cref = new ControlRef(crefNode);
                    this.ControlRefs.Add(cref);
                }

                foreach (XmlNode oaNode in node.SelectNodes("OverflowArea"))
                {
                    OverflowArea oa = new OverflowArea(oaNode);
                    this.OverflowAreas.Add(oa);
                }

                foreach (XmlNode sNode in node.SelectNodes("Strip"))
                {
                    Strip strip = new Strip(sNode);
                    this.Strips.Add(strip);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
		}

		public void AddNode(XmlNode sectionNode)
		{
            try
            {
                XmlNode rowNode = XmlHelper.AddNode(sectionNode, "Row");

                foreach (ControlRef cRef in this.ControlRefs)
                {
                    cRef.AddNode(rowNode);
                }

                foreach (OverflowArea oa in this.OverflowAreas)
                {
                    oa.AddNode(rowNode);
                }

                foreach (Strip s in this.Strips)
                {
                    s.AddNode(rowNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
		}

		public void BuildTreeViewElement(TreeNode sectionNode)
		{
            try
            {
                TreeNode rowNode = new TreeNode("Row");
                rowNode.Tag = this;

                foreach (ControlRef cRef in this.ControlRefs)
                {
                    cRef.BuildTreeViewElement(rowNode);
                }

                foreach (OverflowArea oa in this.OverflowAreas)
                {
                    oa.BuildTreeViewElement(rowNode);
                }

                foreach (Strip s in this.Strips)
                {
                    s.BuildTreeViewElement(rowNode);
                }

                sectionNode.Nodes.Add(rowNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
		}

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Control Ref");
            ToolStripMenuItem tsmi2 = new ToolStripMenuItem("Add Overflow Area");
            ToolStripMenuItem tsmi3 = new ToolStripMenuItem("Add Strip");

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_ItemClicked);
            cms.Tag = relatedNode;
            cms.Items.Add(tsmi);
            cms.Items.Add(tsmi2);
            cms.Items.Add(tsmi3);

            return cms;
        }

		void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
            try
            {
                TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

                switch (e.ClickedItem.Text)
                {
                    case "Add Control Ref":
                        ControlRef cRef = new ControlRef();
                        cRef.BuildTreeViewElement(selectedNode);
                        this.ControlRefs.Add(cRef);
                        break;
                    case "Add Overflow Area":
                        OverflowArea oa = new OverflowArea();
                        oa.BuildTreeViewElement(selectedNode);
                        this.OverflowAreas.Add(oa);
                        break;
                    case "Add Strip":
                        Strip s = new Strip();
                        s.BuildTreeViewElement(selectedNode);
                        this.Strips.Add(s);
                        break;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
		}

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is ControlRef)
                {
                    this.ControlRefs.Remove((ControlRef)o);
                }
                else if (o is OverflowArea)
                {
                    this.OverflowAreas.Remove((OverflowArea)o);
                }
                else if (o is Strip)
                {
                    this.Strips.Remove((Strip)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
